using System;
using System.Web;
using System.Web.Mvc;
using Manazra.Models.Repository;
using Manazra.Helpers;
using Manazra.Models;
using Manazra.Controllers.Filters;

namespace Manazra.Controllers
{
    [BasePreControllerActionFilter]
    public class RepliesController : Controller
    {
        //replies repository
        private ReplyRepository replyRep = new ReplyRepository();

        //adds a reply
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Add(int id, string content)
        {
            ControllerStatus result = new ControllerStatus(0, "");
            //get authentication object from the view data
            UserAuth UserAuth = (UserAuth)ViewData["UserAuth"];
            //only a logged in user can log a reply
            if (UserAuth.Authenticated)
            {
                //get parent segment
                Comment comment = replyRep.GetParent(id);
                if (comment != null)
                {
                    //create new reply
                    Reply reply = new Reply();

                    //set its properties
                    reply.PostedBy = UserAuth.UserName;
                    reply.PostedDate = DateTime.Now;
                    reply.Content = content;

                    //add
                    comment.Replies.Add(reply);
                    //add points to article for this reply
                    replyRep.AddPoints(reply);

                    //save changes
                    replyRep.Save();

                    //for facebook users we publish the news for the reply to facebook
                    if (UserAuth.IsFacebookUser)
                    {
                        new FBPublisher().PublishNewReplyFeed(UserAuth, reply);
                    }

                    //set return status
                    result.Status = 1;
                    result.Message = reply.RecId + "-" + UserAuth.FormattedUserName + " wrote at " + String.Format("{0:g}", reply.PostedDate);
                }
                else
                    result.Message = "Parent (" + id + ") not found";
            }
            else
                result.Message = "User not logged in";

            return Json(result);
        }

        //deletes a reply
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Delete(int id)
        {
            ControllerStatus result = new ControllerStatus(0, "");
            //get parent comment
            var reply = replyRep.GetById(id);
            if (reply != null)
            {
                //access is restricted to authors
                //get authentication object from the view data
                UserAuth UserAuth = (UserAuth)ViewData["UserAuth"];
                if (UserAuth.Authenticated && reply.PostedBy.Equals(UserAuth.UserName, StringComparison.InvariantCulture))
                {
                    //delete response
                    replyRep.Delete(reply);
                    //delete points from article for this reply
                    replyRep.DelPoints(reply);
                    //commit changes
                    replyRep.Save();
                    //set return status
                    result.Status = 1;
                    result.Message = "success";
                }
                else
                    result.Message = "Access Denied";
            }
            else
                result.Message = "Reply (" + id + ") not found";

            return Json(result);
        }
    }
}
